﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;

public partial class DoanhNghiepND : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_Data_LoaiMatHang();
            string idMatHang = Request.Params["Id"];
            string idSubMatHang = Request.Params["IdSubMatHang"];

            if (idMatHang != null && idSubMatHang != null)
            {
                try
                {
                    int idMh = Int32.Parse(idMatHang);
                    int idSubMh = Int32.Parse(idSubMatHang);

                    Bind_Data_DoanhNghiepND(idMh, idSubMh);
                }
                catch (Exception ex)
                {
                    
                }
            }
        }
    }

    /// <summary>
    /// Bindding du lieu cac LoaiMatHang thanh cac tuy chon Menu hien thi o trang
    /// Doanh-Nghiep.aspx
    /// </summary>
    protected void Bind_Data_LoaiMatHang()
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " and TrangThai = 1";
        string strHTML = "";

        TList<LoaiMatHang> Tlist_LoaiMatHang = DataRepository.LoaiMatHangProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);
        if (Tlist_LoaiMatHang.Count > 0)
        {
            for (int i = 0; i < Tlist_LoaiMatHang.Count; i++)
            {
                strHTML += "<li>";
                strHTML += "<a href='" + Tlist_LoaiMatHang[i].Id + "_" + GetTieuDe(Tlist_LoaiMatHang[i].TenMatHang) + "_MatHang.aspx'>";
                strHTML += Tlist_LoaiMatHang[i].TenMatHang;
                strHTML += "</a>";
                strHTML += "</li>";                
                idLoaiMH.Text = strHTML;
            }
        }
    }

    protected void Bind_Data_DoanhNghiepND(int idMathang, int idSubMathang)
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " and Id = " + idMathang;

        TList<MatHang> Tlist_MatHang = DataRepository.MatHangProvider.GetPaged(WhereClause, "Id DESC", 0, 1, out ToTalRows);
        if (Tlist_MatHang.Count > 0)
        {
            ltrTieude.Text = Tlist_MatHang[0].TieuDe;
            dlNoidungDN.DataSource = Tlist_MatHang;
            dlNoidungDN.DataBind();
        }
        
    }

    /// <summary>
    /// Phuong thuc chuyen tieu de co dau thanh khong dau de dua vao link
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    /// <summary>
    /// Format cat chuoi
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }
}